package com.jiulan.hearthearing.ui.course.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseVPFragment;
import com.jiulan.hearthearing.constants.UrlConstants;
import com.jiulan.hearthearing.http.HttpRequest;
import com.jiulan.hearthearing.http.HttpStringCallBack;
import com.jiulan.hearthearing.ui.cart.activity.CartActivity;
import com.jiulan.hearthearing.ui.course.activity.CourseDetailsFMActivity;
import com.jiulan.hearthearing.ui.course.adapter.CourseFMMuluAdapter;
import com.jiulan.hearthearing.ui.course.model.CartAddModel;
import com.jiulan.hearthearing.ui.course.model.CourseDetailsModel;
import com.jiulan.hearthearing.ui.pay.activity.SettlementDeskActivity;
import com.shehuan.niv.NiceImageView;

import java.util.HashMap;
import java.util.Map;

public class CourseDetailsFMFragment extends BaseVPFragment implements View.OnClickListener {
    private Context context;
    private int id;
    private CourseDetailsModel model;
    public CourseFMMuluAdapter muluAdapter;
    private int chooseId = -1;

    private RecyclerView rcvMulu;
    private NiceImageView nivTeacher;
    private TextView tvTeacherName;
    private TextView tvTeacherZhicheng;
    private TextView tvCourseLiangdian;
    private TextView tvPrice;
    private TextView tvYuanjia;
    private TextView tvAddCart;
    private TextView tvBaoming;
    private RelativeLayout rlBottomBar;

    public static CourseDetailsFMFragment newInstance(int id) {
        Bundle args = new Bundle();
        args.putInt("id", id);
        CourseDetailsFMFragment pageFragment = new CourseDetailsFMFragment();
        pageFragment.setArguments(args);
        return pageFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getArguments().getInt("id");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_course_details_fm, container, false);
        context = getActivity();
        initView(view);
        return view;
    }

    private void initView(View view) {
        rcvMulu = view.findViewById(R.id.rcvMulu);
        nivTeacher = view.findViewById(R.id.nivTeacher);
        tvTeacherName = view.findViewById(R.id.tvTeacherName);
        tvTeacherZhicheng = view.findViewById(R.id.tvTeacherZhicheng);
        tvCourseLiangdian = view.findViewById(R.id.tvCourseLiangdian);
        tvPrice = view.findViewById(R.id.tvPrice);
        tvYuanjia = view.findViewById(R.id.tvYuanjia);
        tvAddCart = view.findViewById(R.id.tvAddCart);
        tvBaoming = view.findViewById(R.id.tvBaoming);
        rlBottomBar = view.findViewById(R.id.rlBottomBar);
        rcvMulu.setLayoutManager(new LinearLayoutManager(context));
        tvAddCart.setOnClickListener(this);
        tvBaoming.setOnClickListener(this);
        getData();
    }

    private void setView() {
        if (model.getData().getIs_buy() == 0) {//未购买
            rlBottomBar.setVisibility(View.VISIBLE);
        } else if (model.getData().getIs_buy() == 1) {//已购买
            rlBottomBar.setVisibility(View.GONE);
        }
        Glide.with(context).load(model.getData().getLecturer_info().getPic()).into(nivTeacher);
        tvTeacherName.setText(model.getData().getLecturer_info().getName());
        tvTeacherZhicheng.setText(model.getData().getLecturer_info().getTitle());
        tvCourseLiangdian.setText(model.getData().getLecturer_info().getShort_desc());
        muluAdapter = new CourseFMMuluAdapter(model.getData().getClass_list());
        if (model.getData().getClass_list().size() > 0) {
            chooseId = model.getData().getClass_list().get(0).getId();
            muluAdapter.setClickPosition(chooseId);
        }
        rcvMulu.setAdapter(muluAdapter);
        tvPrice.setText("券后" + String.valueOf(model.getData().getCourse().getPrice()) + "学习币");
        tvYuanjia.setText("原价" + String.valueOf(model.getData().getCourse().getDel_price()) + "学习币");
        tvYuanjia.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); //中划线
        if (model.getData().getClass_list().size() > 0) {
            sendBroadcast(1, 0);
        } else {
            sendBroadcast(1, -1);
        }
        muluAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                sendBroadcast(2, position);
                chooseId = model.getData().getClass_list().get(position).getId();
                muluAdapter.setClickPosition(chooseId);
                muluAdapter.notifyDataSetChanged();
            }
        });
    }

    private void sendBroadcast(int action, int position) {
        Intent intent = new Intent(CourseDetailsFMActivity.LOCAL_BROADCAST);
        intent.putExtra("action", action);
        intent.putExtra("position", position);
        intent.putExtra("courseInfo", new Gson().toJson(model));
        CourseDetailsFMActivity.localBroadcastManager.sendBroadcast(intent);
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {

    }

    private void getData() {
        /**
         * 课程详情
         */
        new HttpRequest(context).doGet(UrlConstants.course_details + String.valueOf(id), "", new HashMap(), CourseDetailsModel.class, new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                if (result instanceof CourseDetailsModel) {
                    model = (CourseDetailsModel) result;
                    setView();
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }

    @Override
    protected void onFragmentFirstVisible() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvAddCart:
                addCart(0);
                break;
            case R.id.tvBaoming:
                addCart(1);
                break;
        }
    }

    /**
     * 加入购物车
     */
    private void addCart(final int type) {
        Map<String, String> map = new HashMap<>();
        map.put("productId", String.valueOf(id));
        map.put("cartNum", "1");
        map.put("uniqueId", "");
        map.put("producttype", "video");
        map.put("new", String.valueOf(type));
        new HttpRequest(context).doPost(UrlConstants.cart_add, "", map, CartAddModel.class, new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                if (result instanceof CartAddModel) {
                    CartAddModel cartAddModel = (CartAddModel) result;
                    switch (type) {
                        case 0:
                            CartActivity.actionStart(context);
                            break;
                        case 1:
                            SettlementDeskActivity.actionStart(context, cartAddModel.getData().getCartId());
                            break;
                    }
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }
}
